package com.yuke.cloud.service.wmc.service.impl;

import com.yuke.cloud.common.core.support.BaseService;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.api.smc.dto.ShopInfoDto;
import com.yuke.cloud.service.api.smc.service.SmcFeignApi;
import com.yuke.cloud.service.api.uac.dto.UserInfoDto;
import com.yuke.cloud.service.api.uac.service.UacUserFeignApi;
import com.yuke.cloud.service.wmc.dao.AssGoodsReturnMapper;
import com.yuke.cloud.service.wmc.dto.AssGoodsReturnDto;
import com.yuke.cloud.service.wmc.entity.AssGoodsReturn;
import com.yuke.cloud.service.wmc.service.AssGoodsReturnService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;


/**
 * Created by zys on 2018/12/10.
 */
@Service
@Transactional
public class AssGoodsReturnServiceImpl extends BaseService<AssGoodsReturn> implements AssGoodsReturnService {
    @Resource
    private AssGoodsReturnMapper assGoodsReturnMapper;
    @Resource
    private UacUserFeignApi uacUserFeignApi;
    @Resource
    private SmcFeignApi smcFeignApi;


    @Override
    public List<AssGoodsReturn> selectByCondition(AssGoodsReturnDto assGoodsReturnDto) {
        List<AssGoodsReturn> list = assGoodsReturnMapper.findByCondition(assGoodsReturnDto);

        if (list==null||list.size()==0){
            return null;
        }

        List<Long> userList = new ArrayList<>();
        List<Long> shopIds = new ArrayList<>();
        for (AssGoodsReturn assGoodsReturn : list) {
            userList.add(assGoodsReturn.getUserId());
            shopIds.add(assGoodsReturn.getShopId());
        }

        Wrapper<List<UserInfoDto>> userInfoByUserIds = uacUserFeignApi.getUserInfoByUserIds(userList);
        List<UserInfoDto> result = userInfoByUserIds.getResult();

        List<ShopInfoDto> shopInfoByShopIds = smcFeignApi.getShopNameByShopIds(shopIds);

        for (AssGoodsReturn assGoodsReturn : list) {
            if (result!=null&&result.size()>0) {
                for (UserInfoDto userInfoDto : result) {
                    if (userInfoDto.getUserId().equals(assGoodsReturn.getUserId())) {
                        assGoodsReturn.setUserName(userInfoDto.getUserName());
                    }
                }
            }
            if (shopInfoByShopIds!=null&&shopInfoByShopIds.size()>0) {
                for (ShopInfoDto shopInfoByShopId : shopInfoByShopIds) {
                    if (shopInfoByShopId.getShopId().equals(assGoodsReturn.getShopId())) {
                        assGoodsReturn.setShopName(shopInfoByShopId.getShopName());
                    }
                }
            }
        }
        return list;
    }
}
